const app = getApp()
// pages/login/index.js
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    Uname: '',
    Upwd: '',
    LY: '小程序',
    txt: '',
    ckmmt: true,
    isShow: false,
    ckmm: 'icon-yanjing1',
    login: 'tel',
    codename: '获取验证码',
    Tel: '',
    inputYzm: '',
    ip: '124.239.176.230',
    zcly: '',
    disabled: true,
    regbg: false,
    sbtype: 0,
    sbname: 'android',
    showys: false,
    focusId: 'name',
    forceSource: '',
  },
  changeLogin: function (e) {
    // if (this.data.checked != 'cb') {
    //   wx.showToast({
    //     title: '请阅读并勾选用户协议',
    //     icon: 'none',
    //   })
    //   return;
    // }
    if (e.currentTarget.dataset.login != this.data.login) {
      this.setData({
        login: e.currentTarget.dataset.login
      })
    }
  },
  inputTel: function (e) {
    this.setData({
      Tel: e.detail.value
    })
  },
  inputYzm: function (e) {
    this.setData({
      inputYzm: e.detail.value
    })
  },
  inputUser: function (e) {
    this.setData({
      Uname: e.detail.value
    })
  },
  inputPass: function (e) {
    this.setData({
      Upwd: e.detail.value
    })
  },
  loginckmm: function () {
    if (this.data.ckmmt) {
      this.setData({
        ckmmt: false,
        ckmm: 'icon-yanjing'
      })
    } else {
      this.setData({
        ckmmt: true,
        ckmm: 'icon-yanjing1'
      })
    }
  },
  logingetyzmN: function () {
    // body...
  },
  logingetyzm: function () {

    var that = this;
    var tel = this.data.Tel;
    var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (that.data.checked != 'cb') {
      wx.showToast({
        title: '请阅读并勾选用户协议',
        icon: 'none',
      })
      return;
    }
    if (!myreg.test(tel)) {
      app.tishi('请输入正确的手机号');
      return false;
    } else {
      var api_input = {
        tel: this.data.Tel
      }
      wx.showToast({
        title: "正在加载..",
        icon: "loading",
        duration: 100000
      })
      app.getDaoPostN('api/FeijiuApp/PostSendYzm_SMS_New', api_input, function (r) {
        if (r.code == 200) {
          wx.hideToast();
          app.tishi(r.data.Mess);
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: true,
              })
            } else {
              that.setData({
                codename: num + "s后重新获取",
                disabled: false,
              })
            }
          }, 1000)
        } else {
          app.tishi(r.message)
        }
      })
    }
  },
  loginBtn: function () {

    console.log(1)
    var that = this
    if (that.data.login == 'user') {
      if (that.data.Uname.length < 1) {
        app.tishi('请输入您的帐号/绑定帐号的手机号')
        return false
      }
      if (that.data.Upwd.length < 1) {
        app.tishi('请输入密码')
        return false
      }

      if (that.data.checked != 'cb') {
        wx.showToast({
          title: '请阅读并勾选用户协议',
          icon: 'none',
        })
        return;
      }
      var api_input = {
        Uname: this.data.Uname,
        Upwd: this.data.Upwd,
        LY: 1,
        IP: this.data.ip,
        sbid: '',
        sbtype: this.data.sbtype,
        sbname: this.data.sbname
      }
      // app.getDaoPost('api/FeijiuApp/Post_FJ_LoginLog',api_input,function (res) {
      //   if (res.code == 200) {
      //     wx.setStorageSync('FjLoginLog',res.data);
      //     wx.navigateBack();
      //   }else{
      //     app.tishi(res.message)       
      //   }
      // })

      app.getDaoPost('api/FeijiuApp/Feijiu_Login', api_input, function (r) {
        if (r.code == 200) {
          if (r.data.Mess == '为了你的账户安全，需要验证你的身份信息！') {
            wx.showToast({
              title: '为了你的账户安全，需要验证你的身份信息！',
              icon: 'none'
            })
            var TelList = r.data.YZMobile.split(",")
            console.log(TelList)
            wx.setNavigationBarTitle({
              title: '请验证手机号码',
            })
            that.setData({
              login: 'tel2',
              TelList: TelList
            })
            return;
          }
          wx.setStorageSync('FjLoginLog', r.data);
          that.go_back()
        } else {
          app.tishi(r.message)
        }
        // console.log(r)
      })
    }

    if (this.data.login == 'tel') {
      var that = this
      var tel = this.data.Tel;
      var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
      if (!myreg.test(tel)) {
        app.tishi('请输入正确的手机号');
        return false;
      }
      if (that.data.checked != 'cb') {
        wx.showToast({
          title: '请阅读并勾选用户协议',
          icon: 'none',
        })
        return;
      }
      var api_input = {
        Tel: this.data.Tel,
        YZM: this.data.inputYzm,
        zcly: this.data.zcly,
        LY: this.data.LY,
        IP: this.data.ip,
        sbid: '',
        sbtype: this.data.sbtype,
        sbname: this.data.sbname
      }
      console.log(that.data.login)

      app.getDaoPost('api/FeijiuApp/PostLoginLog_SMS', api_input, function (r) {


        if (r.code == 200) {
          app.tishi(r.message);
          wx.setStorageSync('FjLoginLog', r.data);
          setTimeout(function () {
            that.go_back()
          }, 300)
        } else {
          app.tishi(r.message)
        }
      })
    }
    if (that.data.login == 'tel2') {
      console.log(122)
      that.loginBtn2()
      return;
    }
  },
  loginBtn2: function () {
    var that = this

    if (this.data.login == 'tel2') {
      var that = this
      var tel = this.data.Tel;
      var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
      if (!myreg.test(tel)) {
        app.tishi('请输入正确的手机号');
        return false;
      }
      var api_input = {
        Tel: this.data.Tel,
        YZM: this.data.inputYzm,
        zcly: this.data.zcly,
        LY: this.data.LY,
        IP: this.data.ip,
        sbid: '',
        sbtype: this.data.sbtype,
        sbname: this.data.sbname
      }
      app.getDaoPost('api/FeijiuApp/PostLoginLog_SMS', api_input, function (r) {


        if (r.code == 200) {
          app.tishi(r.message);
          wx.setStorageSync('FjLoginLog', r.data);
          setTimeout(function () {
            that.go_back()
          }, 300)
        } else {
          app.tishi(r.message)
        }
      })
    }
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      Tel: this.data.TelList[e.detail.value]
    })
  },
  toastLogin: function (tishi) {
    var that = this
    that.setData({
      isShow: true,
      txt: 'Feijiu网:' + tishi
    });
    setTimeout(function () {
      that.setData({
        isShow: false
      });
    }, 2500);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pages = getCurrentPages();
    var is_Yins = wx.getStorageSync('is_Yins');
    if (!is_Yins) {
      this.setData({
        showys: true
      })
    }
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2].route;
      if (prevPage == '/packageA/pages/woyaochuzhi/detailed') {
        this.setData({
          zcly: 'chuzhi'
        })
      }
    }
    var that = this;
    wx.request({
      url: 'https://zsbpt.zaisubao.cn/index.php/Api/Feijiu/getIp',
      data: {},
      method: 'get',
      success: function (r) {
        that.setData({
          ip: r.data.ip
        })
        wx.setStorageSync('ip', r.data.ip)
      }
    });
    wx.getSystemInfo({
      success: function (e) {
        if (e.platform == 'android') {
          that.setData({
            sbtype: 1
          })
        } else {
          that.setData({
            sbtype: 2
          })
        }
        that.setData({
          sbname: e.brand + e.model
        })
      }
    })
    // 类似于合同的特殊强制判断
    if (options.forceSource) {
      this.setData({
        forceSource: options.forceSource
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    var zhuantiurl = wx.getStorageSync('zhuantiurl');
    if (FjLoginLog.UID && zhuantiurl) {
      wx.navigateTo({
        url: '../other/otherWeb',
        success: function (res) {
          // 通过 eventChannel 向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', {
            url: zhuantiurl,
            pageSrc: 3
          })
        }
      })
    }
  },

  FeijiuReg: function () {
    this.setData({
      regbg: true
    })
  },
  mimazhaohui: function () {
    wx.navigateTo({
      url: 'passback'
    })
  },
  toyszc: function () {
    wx.navigateTo({
      url: 'toweb?url=https://apps.feijiu.net/app/yssm/'
    })
  },
  tozcxy: function () {
    wx.navigateTo({
      url: 'toweb?url=https://apps.feijiu.net/app/fwxy'
    })
  },
  error: function () {
    this.setData({
      regbg: false
    })
  },
  sucess: function () {
    this.setData({
      regbg: false
    })
    wx.navigateTo({
      url: 'reg'
    })
  },
  bindGetUserInfo: function (e) {
    if (this.data.checked != 'cb') {
      wx.showToast({
        title: '请阅读并勾选用户协议',
        icon: 'none',
      })
      return;
    }
    console.log('loginUSER')
    var that = this
    var info = e.detail;
    var openid = wx.getStorageSync('openid');
    if (info.errMsg == 'getUserInfo:ok') {
      var api_input = {
        Ly: 1,
        UnionID: openid.unionid,
        OpenID: openid.openid,
        nickName: info.userInfo.nickName,
        AesData: info.encryptedData,
        AesIv: info.iv,
        AesKey: openid.session_key,
        IP: this.data.ip,
        sbid: '',
        sbtype: this.data.sbtype,
        sbname: this.data.sbname
      }
      app.getDaoPostN('api/FeijiuApp/PostLogin_WxLog', api_input, function (r) {
        if (r.code == 200) {
          if (r.data.IsDl == 0) {
            app.tishi('登录失败，该微信未绑定Feijiu账号!');
            wx.setStorageSync('userinfo', info);
            wx.navigateTo({
              url: 'binduser'
            })
          } else {
            app.tishi(r.data.Mess);
            wx.setStorageSync('FjLoginLog', r.data);
            setTimeout(function () {
              that.go_back()
            }, 2000)
          }
        } else {
          app.tishi('微信登录失败!')

        }
      })

    } else {
      app.tishi('微信登录失败,请允许Feijiu网获取您的微信信息!')
    }
  },
  checkboxChange(e) {
    console.log(e)
    this.setData({
      checked: e.detail.value[0]
    })
  },
  to_index() {
    wx.switchTab({
      url: '../index/index',
    })
  },


  to_guanbi() {
    this.setData({
      showys: false
    })
    wx.setStorageSync('is_Yins', 1);
  },

  go_back() {
    if (this.data.forceSource) {
      wx.reLaunch({
        url: this.data.forceSource
      });
      return
    }
    var pages = getCurrentPages();
    if (pages.length === 1 && (pages[0].route === 'pages/login/index')) {
      this.to_index()
    } else {
      wx.navigateBack({
        delta: 2
      })
    }
  }
})